import { Pool } from './Pool'

/**
 * Redis连接池
 */
export const redis = new Pool({
    host: '127.0.0.1',
    port: 6379,
    password: '9bq2d3c5m8f2',
    db: 4,
    pool: {
        min: 0,
        max: 10,
    },
})

export const redisUtils = {
    get: async <T>(key: string): Promise<T | undefined> => {
        const data = await redis.get(key)
        if (typeof data === 'string') {
            return JSON.parse(data)
        }
    },
    set: async (key: string, value: any) => {
        if (typeof value === 'undefined' || value === null) {
            await redis.del(key)
        } else {
            await redis.set(key, JSON.stringify(value))
        }
    },
    del: (key: string) => redis.del(key),
}
